package com.projects.android.views;

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.projects.android.R;
import com.projects.android.adapters.ManageOrderDetailsAdapter;
import com.projects.android.dto.OrderDetailDTO;
import com.projects.android.utils.SelectedItems;
import com.projects.android.utils.Utils;

public class OrderDetails extends ListActivity {
	private TextView lblHeaderFood;
	private TextView lblHeaderQuantity;
	private TextView lblHeaderPrice;
	private TextView lblFieldTotal;
	private TextView lblTotal;
	
	private TextView lblOrderDate;
	private TextView lblShipDate;
	private TextView lblIsVerified;
	private TextView lblIsShipped;
	private TextView lblDeliveryNote;
	private TextView lblReceiverAddress;
	private TextView lblReceiverName;
	private TextView lblReceiverPhone;

	public static ArrayList<OrderDetailDTO> Details;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.orderdetails);
		initTable();
		loadData();
	}

	public void initTable() {
		lblHeaderFood = (TextView) findViewById(R.id.lblHeaderFood);
		lblHeaderQuantity = (TextView) findViewById(R.id.lblHeaderQuantity);
		lblHeaderPrice = (TextView) findViewById(R.id.lblHeaderPrice);
		lblFieldTotal = (TextView) findViewById(R.id.lblFieldTotal);
		lblTotal = (TextView) findViewById(R.id.lblTotal);
		
		lblOrderDate = (TextView) findViewById(R.id.lblOrder_OrderDate);
		lblShipDate = (TextView) findViewById(R.id.lblOrder_ShipDate);
		lblIsVerified = (TextView) findViewById(R.id.lblOrder_IsVerified);
		lblIsShipped = (TextView) findViewById(R.id.lblOrder_IsShipped);
		lblDeliveryNote = (TextView) findViewById(R.id.lblOrder_DeliveryNote);
		lblReceiverAddress = (TextView) findViewById(R.id.lblOrder_ReceiverAddress);
		lblReceiverName = (TextView) findViewById(R.id.lblOrder_ReceiverName);
		lblReceiverPhone = (TextView) findViewById(R.id.lblOrder_ReceiverPhone);

		lblHeaderFood.getLayoutParams().width = Utils.SCREEN_WIDTH / 2;
		lblHeaderQuantity.getLayoutParams().width = Utils.SCREEN_WIDTH / 4;
		lblHeaderPrice.getLayoutParams().width = Utils.SCREEN_WIDTH / 4;
		lblFieldTotal.getLayoutParams().width = Utils.SCREEN_WIDTH / 2;
		lblTotal.getLayoutParams().width = Utils.SCREEN_WIDTH / 2;
	}

	public void loadData() {
		if (Details != null) {
			ManageOrderDetailsAdapter adapter = new ManageOrderDetailsAdapter(
					this, R.layout.orderdetailitem, R.id.lblOrderDetailFood,
					Details);
			setListAdapter(adapter);
			getListView().setTextFilterEnabled(true);

			// Update total
			double sum = 0;
			for (OrderDetailDTO detail : Details) {
				sum += (detail.getQuantity() * detail.getPrice());
			}
			lblTotal.setText(String.valueOf(Utils.round(sum, 2)));
			
			lblOrderDate.setText(SelectedItems.SelectedOrder.getOrderDate());
			lblShipDate.setText(SelectedItems.SelectedOrder.getShipDate() == null ? "" : SelectedItems.SelectedOrder.getShipDate());
			lblIsVerified.setText(SelectedItems.SelectedOrder.getIsVerified() ? "true" : "false");
			lblIsShipped.setText(SelectedItems.SelectedOrder.getIsShipped() ? "true" : "false");
			lblDeliveryNote.setText(SelectedItems.SelectedOrder.getDeliveryNote());
			lblReceiverAddress.setText(SelectedItems.SelectedOrder.getReceiverAddress());
			lblReceiverName.setText(SelectedItems.SelectedOrder.getReceiverName());
			lblReceiverPhone.setText(SelectedItems.SelectedOrder.getReceiverPhone());
		}
	}
}
